/**
 * 0. Project	: com.dnbns.bodywater 프로젝트
 *
 * 1. FileName	: RecordActivity.java
 * 2. Package	: com.dnbns.bodywater
 * 3. 작성자		: 윤화현
 * 4. 작성일		: 2013. 12. 4. 오후 4:32:10
 * 5. Comment	: 
 */

package com.dnbns.bodywater;

import java.util.ArrayList;
import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dnbns.bodywater.adapter.RecordAdapter;
import com.dnbns.bodywater.bean.RecordBean;
import com.dnbns.lib.CodeUtil;

public class RecordActivity extends Activity {
	
	private Calendar cal;
	
	private int mYear;
	private int mMonth;
	private int mDay;
	private String mToday;
	private int mTotalDrink;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_record);
		
		init();
		initTop();
	}

/**
 ***************************************************************
 * init
 ***************************************************************
 */
	
	private void init(){
		cal = Calendar.getInstance();

		mYear = cal.get(Calendar.YEAR);
		mMonth = cal.get(Calendar.MONTH)+1;
		mDay = cal.get(Calendar.DAY_OF_MONTH);
		
		mToday = mYear+"-"+mMonth+"-"+mDay;
		
		TextView year = (TextView)findViewById(R.id.year_txt);
		TextView month = (TextView)findViewById(R.id.month_txt);
		TextView day = (TextView)findViewById(R.id.day_txt);
		
		year.setText(mYear+"");
		month.setText(mMonth+"");
		day.setText(mDay+"");

		ImageView prev = (ImageView)findViewById(R.id.prev);
		ImageView next = (ImageView)findViewById(R.id.next);
		ImageView today = (ImageView)findViewById(R.id.today_btn);
		
		prev.setOnClickListener(OCL);
		next.setOnClickListener(OCL);
		today.setOnClickListener(OCL);
		prev.setOnTouchListener(OTL);
		next.setOnTouchListener(OTL);
		today.setOnTouchListener(OTL);
		
		ListView list = (ListView)findViewById(R.id.record_list);
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
				// TODO Auto-generated method stub
				final RecordAdapter mAdapter = (RecordAdapter) adapter.getAdapter();
				final RecordBean mBean = (RecordBean) adapter.getAdapter().getItem(position);

				AlertDialog.Builder ab = new AlertDialog.Builder(RecordActivity.this);
				ab.setTitle("삭제");
				ab.setMessage("삭제하시겠습니까?");
				ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						try {
							CodeUtil.getInstance(getApplicationContext()).deleteCode(1000, mBean.getIdx()+"");
							mAdapter.remove(mBean);
							mTotalDrink -= mBean.getCup_vol();
							TextView ml = (TextView)findViewById(R.id.record_ml);
							ml.setText("합계 "+mTotalDrink+"ml");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						mAdapter.notifyDataSetInvalidated();
						dialog.dismiss();
					}
				});
				ab.setNegativeButton("취소", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
				ab.show();
			}
		});

		getDBWater();
	}
	
	private void initTop(){
		ImageView back = (ImageView)findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

/**
 ***************************************************************
 * init
 ***************************************************************
 */
	
	private void setListData(){
		TextView year = (TextView)findViewById(R.id.year_txt);
		TextView month = (TextView)findViewById(R.id.month_txt);
		TextView day = (TextView)findViewById(R.id.day_txt);

		mYear = cal.get(Calendar.YEAR);
		mMonth = cal.get(Calendar.MONTH)+1;
		mDay = cal.get(Calendar.DAY_OF_MONTH);
		
		year.setText(mYear+"");
		month.setText(mMonth+"");
		day.setText(mDay+"");

		getDBWater();
	}
	
    //DB에서 데이터 select.
    private void getDBWater(){
    	mTotalDrink = 0;
    	JSONArray objA = CodeUtil.getInstance(getApplicationContext()).getQuery("water_record", mYear+"", mMonth+"", mDay+"");
    	ArrayList<RecordBean> mList = new ArrayList<RecordBean>();
    	
		try {
	    	for(int i=0; i<objA.length(); i++){
	    		RecordBean mBean = new RecordBean();
	    		mTotalDrink += objA.getJSONObject(i).getInt("cup_vol");
	    		mBean.setIdx(objA.getJSONObject(i).getInt("idx"));
	    		mBean.setCup_vol(objA.getJSONObject(i).getInt("cup_vol"));
	    		mBean.setHour(objA.getJSONObject(i).getInt("hour"));
	    		mBean.setMinute(objA.getJSONObject(i).getInt("minute"));
	    		mList.add(mBean);
	    	}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ListView list = (ListView)findViewById(R.id.record_list);
		
		RecordAdapter mAdapter = new RecordAdapter(getApplicationContext(), mList);
		list.setAdapter(mAdapter);
		mAdapter.notifyDataSetInvalidated();
		
		TextView ml = (TextView)findViewById(R.id.record_ml);
		ml.setText("합계 "+mTotalDrink+"ml");
		
		objA = null;
    }

/**
 ***************************************************************
 * Event
 ***************************************************************
 */
	

	View.OnClickListener OCL = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.prev:
				cal.add(Calendar.DAY_OF_MONTH, -1);
				setListData();
				break;
			case R.id.next:
				if(mToday.equals(mYear+"-"+mMonth+"-"+mDay)){
					return;
				}else{
					cal.add(Calendar.DAY_OF_MONTH, +1);
					setListData();
				}
				break;
			case R.id.today_btn:
				String[] temp = mToday.split("-"); 
				cal.set(Integer.valueOf(temp[0]), Integer.valueOf(temp[1])-1, Integer.valueOf(temp[2]));
				setListData();
				break;
			default:
				break;
			}
			
		}
	};
	
	View.OnTouchListener OTL = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				switch (v.getId()) {
				case R.id.prev:
					((ImageView)v).setImageResource(R.drawable.record_left_on);
					break;
				case R.id.next:
					((ImageView)v).setImageResource(R.drawable.record_right_on);
					break;
				case R.id.today_btn:
					((ImageView)v).setImageResource(R.drawable.record_today_on);
					break;
				}
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				switch (v.getId()) {
				case R.id.prev:
					((ImageView)v).setImageResource(R.drawable.record_left);
					break;
				case R.id.next:
					((ImageView)v).setImageResource(R.drawable.record_right);
					break;
				case R.id.today_btn:
					((ImageView)v).setImageResource(R.drawable.record_today);
					break;
				}
			}
			return false;
		}
	};
}
